package com.wokeMy.view.Lottery;

import android.app.Dialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wokeMy.view.adpter.LotterAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.DoubleLo;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class OpenLottActivity extends BaseActivity {


    private ImageView recordopenback;
    private ListView recordopenlv;

    Dialog mydialog;
    ArrayList<DoubleLo> data;
    LotterAdapter lotterAdapter;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_open_lott);

    }

    @Override
    public void initView() {
        this.recordopenlv = (ListView) findViewById(R.id.record_open_lv);
        this.recordopenback = (ImageView) findViewById(R.id.record_open_back);
    }

    @Override
    public void initData() {
        data=new ArrayList<>();
        String lotteryid = getIntent().getStringExtra("lotteryid");
        mydialog= Util.createLoadingDialog(OpenLottActivity.this);
        queryLotteryList(lotteryid);
    }

    @Override
    public void initActivity() {
        lotterAdapter=new LotterAdapter(getApplicationContext(),data);
        recordopenlv.setAdapter(lotterAdapter);
        recordopenback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    // 查询奖期列表
    public void  queryLotteryList(String lotteryid){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("lotteryid",lotteryid);//玩法代码 118//双色球
        Log.e("查询奖期列表params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CPLIST_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(mydialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("查询奖期列表result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            Gson gson = new Gson();
                            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
                            for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                DoubleLo doubleLo = gson.fromJson(jsonArray.getString(i1), DoubleLo.class);
                                // status -1新奖期可预售 0新奖期开期通知 2奖期期结通知  3奖期开始开奖通知 7奖期开奖完成通知
                                if (!TextUtils.isEmpty(doubleLo.getBonuscode())){
                                    Log.e("doubleLo.getStatus()",""+i1);
                                    data.add(doubleLo);
                                }
                            }
                            Log.e("doubleLo.getStatus()",""+data.toString());
                            lotterAdapter.notifyDataSetChanged();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }

}
